//
//  SAPaperSetCollectionViewCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/5/17.
//

#import "SAPaperSetCollectionViewCell.h"
#import "SAPaperSetModel.h"

@interface SAPaperSetCollectionViewCell ()

@property (nonatomic, strong) UIButton *selectBtn;
@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIView *btnView;
@property (nonatomic, strong) UIButton *littleBtn;
@property (nonatomic, strong) UIButton *bigBtn;
@property (nonatomic, strong) UIButton *sortBtn;

@end

@implementation SAPaperSetCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame {
    if(self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

-(void)setModel:(SAPaperSetModel *)model {
    _model = model;
    self.titleLab.text = model.titleStr;
    self.selectBtn.selected = model.isSelect;
    
    if ([model.fontStr containsString:@"+"]) {
        self.littleBtn.selected = YES;
        self.bigBtn.selected = NO;
        self.littleBtn.backgroundColor = kLineColor;
        self.bigBtn.backgroundColor = kBuleColor;
    } else {
        self.littleBtn.selected = NO;
        self.bigBtn.selected = YES;
        self.littleBtn.backgroundColor = kBuleColor;
        self.bigBtn.backgroundColor = kLineColor;
    }
    
    self.btnView.hidden = [self.model.titleStr isEqualToString:@"条码"];
}

#pragma mark - event
-(void)clickedSelectBtn {
    if (self.model.isSelect != YES) {
        !self.selectBlock ?: self.selectBlock();
    }else {
        self.model.isSelect = NO;
        self.selectBtn.selected = NO;
    }
}

-(void)clickedLittleBtn {
    self.littleBtn.selected = NO;
    self.bigBtn.selected = YES;
    self.littleBtn.backgroundColor = kBuleColor;
    self.bigBtn.backgroundColor = kLineColor;
    NSString *key = [NSString stringWithFormat:@"%d",abs([self.model.valueStr intValue])];
    self.model.fontStr = [NSString stringWithFormat:@"-%@",key];
}

-(void)clickedBigBtn {
    self.littleBtn.selected = YES;
    self.bigBtn.selected = NO;
    self.littleBtn.backgroundColor = kLineColor;
    self.bigBtn.backgroundColor = kBuleColor;
    NSString *key = [NSString stringWithFormat:@"%d",abs([self.model.valueStr intValue])];
    self.model.fontStr = [NSString stringWithFormat:@"+%@",key];
}

-(void)initUI {
    
    self.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.selectBtn];
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.btnView];
    [self.btnView addSubview:self.littleBtn];
    [self.btnView addSubview:self.bigBtn];
    [self.contentView addSubview:self.sortBtn];
}

-(UIButton *)selectBtn {
    if (!_selectBtn) {
        _selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _selectBtn.frame = SARectMake(0, 0, 60, 45);
        [_selectBtn setImage:[UIImage imageNamed:@"shop_blueRound2"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"shop_blueCheck2"] forState:UIControlStateSelected];
        [_selectBtn addTarget:self action:@selector(clickedSelectBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _selectBtn;
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(60, 0, 100, 45)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(16);
    }
    return _titleLab;
}

-(UIView *)btnView {
    if (!_btnView) {
        _btnView = [[UIView alloc] initWithFrame:SARectMake(kBaseScreenWidth - 130, 5, 80, 35)];
        _btnView.layer.cornerRadius = 5;
        _btnView.layer.masksToBounds = YES;
        _btnView.backgroundColor = kLineColor;
    }
    return _btnView;
}

-(UIButton *)littleBtn {
    if (!_littleBtn) {
        _littleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _littleBtn.frame = SARectMake(0, 0, 40, 35);
        _littleBtn.backgroundColor = kBuleColor;
        [_littleBtn setTitle:@"小" forState:UIControlStateNormal];
        [_littleBtn setTitleColor:kLineColor forState:UIControlStateNormal];
        [_littleBtn setTitleColor:kBuleColor forState:UIControlStateSelected];
        _littleBtn.titleLabel.font = kFont(16);
        _littleBtn.selected = NO;
        [_littleBtn addTarget:self action:@selector(clickedLittleBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _littleBtn;
}

-(UIButton *)bigBtn {
    if (!_bigBtn) {
        _bigBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _bigBtn.frame = SARectMake(40, 0, 40, 35);
        _bigBtn.backgroundColor = kLineColor;
        [_bigBtn setTitle:@"大" forState:UIControlStateNormal];
        [_bigBtn setTitleColor:kLineColor forState:UIControlStateNormal];
        [_bigBtn setTitleColor:kBuleColor forState:UIControlStateSelected];
        _bigBtn.titleLabel.font = kFont(16);
        _bigBtn.selected = YES;
        [_bigBtn addTarget:self action:@selector(clickedBigBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bigBtn;
}

-(UIButton *)sortBtn {
    if (!_sortBtn) {
        _sortBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sortBtn.frame = SARectMake(kBaseScreenWidth - 45, 0, 45, 45);
        [_sortBtn setImage:[UIImage imageNamed:@"pageSet_sort"] forState:UIControlStateNormal];
    }
    return _sortBtn;
}

@end
